library(data.table) #Load all required packages
library(rvest)
library(lubridate)
library(dbplyr)
library(DataComputing)
library(magrittr)
library(Hmisc)
rm(list = ls()) #Clean up environment
This dataset contains policy data for 50 US states and DC from 2001 to 2017. Data include information related to state legislation and regulations on nutrition, physical activity, and obesity in settings such as early care and education centers, restaurants, schools, work places, and others. To identify individual bills, use the identifier ProvisionID. A bill or citation may appear more than once because it could apply to multiple health or policy topics, settings, or states.
getwd() #Get working directory
[1] "/Users/fun.k/Final-Project"
setwd("~/Downloads") #Set working directory
The working directory was changed to /Users/fun.k/Downloads inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.
untidy <-read.csv("CDC_Nutrition__Physical_Activity__and_Obesity_-_Legislation.csv") #Read file in table format and create a data frame from it
gc(reset=TRUE) #Change from data frame to data table
used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells 2094262 111.9 3712468 198.3 NA 2094262 111.9
Vcells 4521648 34.5 10146329 77.5 16384 4521648 34.5
tracemem(untidy)
[1] "<0x107ddf620>"
untidy <- as.data.table(untidy)
tracemem[0x107ddf620 -> 0x107a98890]: copy as.data.table.data.frame as.data.table
tracemem[0x107ddf620 -> 0x10c8f2010]: as.list.data.frame as.list vapply copy as.data.table.data.frame as.data.table
gc()
used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells 2094257 111.9 3712468 198.3 NA 2097965 112.1
Vcells 4521596 34.5 10146329 77.5 16384 4989249 38.1
This dataset can be useful in looking at
untidy #Take a look
#Clean Data
#Change variables to start with lowercase/ make easier to use
Legislation <-
untidy %>%
rename(year = Year, quarter = Quarter, state = LocationAbbr, healthTopic = HealthTopic, policyTopic = PolicyTopic, dataSource = DataSource, setting = Setting, title = Title, status = Status, citation = Citation, statusAltValue = StatusAltValue, dataType = DataType, comments = Comments, enDate = EnactedDate, effDate = EffectiveDate, coordinates = GeoLocation, display = DisplayOrder)
Legislation
We can see how much legislation has been passed in certain years, which years have been the most progressive, which states have more legislation than others, which topic has been most popular, and more. This information can give government officials the ability to see where certain states need to improve, which topics need to be emphasized, and more.
yearlyData <-
Legislation %>%
group_by(year) %>%
summarise(total = sum(year)) %>%
mutate(total = total/1000)
yearlyData %>%
ggplot(aes(x=year,y=total ))+
geom_bar(stat='identity',position='stack', width=.9, fill = "red") +
ylab("Total (1000s)") +
xlab("Year")
Each bar in the chart represents one year. One can see that in 2011, the most legislation/regulation has been passed between 2001 and 2017.
Legislation %>%
group_by(healthTopic, year) %>%
summarise(total = sum(year))
NA
NA